﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Link 的摘要说明
/// </summary>
public class Link
{
	
		 // Builds an absolute URL
    private static string BuildAbsolute(string relativeUri)
    {
        // get current uri
        Uri uri = HttpContext.Current.Request.Url;
        // build absolute path
        string app = HttpContext.Current.Request.ApplicationPath;
        if (!app.EndsWith("/")) app += "/";
        relativeUri = relativeUri.TrimStart('/');
        // return the absolute path
        return HttpUtility.UrlPathEncode(
          String.Format("http://{0}:{1}{2}{3}",
          uri.Host, uri.Port, app, relativeUri));
    }

    // Generate a department URL
    public static string ToDepartment(string departmentId, string page)
    {
        if (page == "1")
            return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}", departmentId));
        else
            return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&Page={1}", departmentId, page));
    }

    // Generate a department URL for the first page
    public static string ToDepartment(string departmentId)
    {
        return ToDepartment(departmentId, "1");
    }

    public static string ToCategory(string departmentId, string categoryId, string page)
    {
        if (page == "1")
            return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}", departmentId, categoryId));
        else
            return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}&Page={2}", departmentId, categoryId, page));
    }

    public static string ToCategory(string departmentId, string categoryId)
    {
        return ToCategory(departmentId, categoryId, "1");
    }

    public static string ToProduct(string productId)
    {
        return BuildAbsolute(String.Format("Product.aspx?ProductID={0}", productId));
    }

    public static string ToProductImage(string fileName)
    {
        // build product URL
        return BuildAbsolute("/ProductImages/" + fileName);
    }//
		//TODO: 在此处添加构造函数逻辑
		//
    public static string search(string word)
    {
        // build product URL
        return BuildAbsolute(String.Format("Catalog.aspx?search={0}", word));
    }//
}